package com.zxl.always.on.display.ui.helper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.base.OnMultiTouchListener;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.model.bean.ClockInfo;
import com.zxl.always.on.display.tools.LogUtils;
import com.zxl.always.on.display.ui.receiver.ScreenReceiver;
import com.zxl.always.on.display.ui.widget.clock.BaseClockView;

/**
 * Created by Administrator on 2018/2/24.
 */

public class ScreenHelper {

    private Context mContext;

    private WindowHelper mWindowHelper;

    private WakeLockHelper mWakeLockHelper;

    private BrightnessHelper mBrightnessHelper;

    private BaseClockView mBaseClockView;

    public ScreenHelper(Context mContext) {
        this.mContext = mContext;
        mWakeLockHelper = new WakeLockHelper(mContext);
        mBrightnessHelper = new BrightnessHelper(mContext);
        mWindowHelper = new WindowHelper(mContext);
        refreshView();
    }

    public void closeScreen() {
        if (null != mBaseClockView) {
            WakeLockHelper.turnScreenOn(mContext);
            mBrightnessHelper.resetBrightness(mContext);
            mWakeLockHelper.release();
            mWindowHelper.remove(mBaseClockView);
            mBaseClockView.pauseStandAloneMode();
            mBaseClockView.stopBatteryMonitor();
        }
    }

    public void showScreen() {
        mWindowHelper.add(mBaseClockView);
        mBaseClockView.setStandAloneMode(true);
        mBaseClockView.startBatteryMonitor();
        mBrightnessHelper.setBrightness();
        mWakeLockHelper.acquire();
    }

    public void refreshView() {
        ClockStyle clockStyle = ClockInfo.getClockStyle();
        LogUtils.d("refreshView clockStyle : " + clockStyle);
        mBaseClockView = (BaseClockView) LayoutInflater.from(mContext).inflate(clockStyle.getLayout(), null);
        mBaseClockView.setOnTouchListener(new OnMultiTouchListener() {
            @Override
            public void onMultiTouch(View v, MotionEvent event, int touchCount) {
                if (touchCount >= 2) {
                    closeScreen();
                    ApplicationHelper.postUiThreadDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ScreenReceiver.mIsShown = false;
                        }
                    }, 800);
                }
            }
        });
        ClockInfo clockInfo = new ClockInfo(clockStyle);
        mBaseClockView.setDigitalStyle(clockInfo.clockPosition);
        mBaseClockView.setPaletteColor(clockInfo.palettePosition);
        mBaseClockView.setBackgroundStyle(clockInfo.backgroundPosition);
    }

}
